//
//  CQNavigationUtiles.m
//  CQFramework
//
//  Created by runo on 16/5/27.
//  Copyright © 2016年 caoqin. All rights reserved.
//

#import "CQNavigationUtiles.h"
#import "AppDelegate.h"

@implementation CQNavigationUtiles

+(void)cqSetNavigationBarTransparent:(UIViewController *)vc IsTransparent:(BOOL)transparent{
    UIImage *bgImg = nil;
    
    if (transparent) {
        bgImg = [UIImage new];
    }
    
    
    //导航栏背景透明
    [vc.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
    //底部那条线透明
    vc.navigationController.navigationBar.shadowImage = bgImg;
}

/**返回上一层*/
+ (void)cqSwitchBack:(NSInteger)layerNumber
{
    AppDelegate *appDelegate = [self cqAppDelegate];
    while (layerNumber > 0 && [[appDelegate.rootNav viewControllers]count] > 0)
    {
        if (layerNumber == 1)
        {
            [appDelegate.rootNav popViewControllerAnimated:YES];
        }else
        {
            [appDelegate.rootNav popViewControllerAnimated:NO];
        }
        
        layerNumber --;
    }
}

//得到根目录的代理
+ (AppDelegate *)cqAppDelegate
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return appDelegate;
}

/**弹出制定的viewController*/
+(void)cqPopViewController:(UIViewController *)controller{

        AppDelegate *appDelegate = [self cqAppDelegate];
        NSMutableArray *arr_t = [NSMutableArray arrayWithArray:[appDelegate.rootNav viewControllers]] ;
        if ([arr_t containsObject:controller]) {
            NSInteger index = [arr_t indexOfObject:controller];
            [arr_t removeObjectAtIndex:index];
        }
        [appDelegate.rootNav setViewControllers:arr_t];

}

/**添加到这个视图为根视图*/
+ (void)switchToRootViewController:(UIViewController *)controller
{
    AppDelegate *appDelegate = [self cqAppDelegate];
    NSArray *controllersArray = [appDelegate.rootNav viewControllers];
    NSInteger count = [controllersArray count];
    for (NSInteger i = 0; i < count; i ++)
    {
        UIViewController *viewcontroller = [controllersArray objectAtIndex:i];
        [self cqPopViewController:viewcontroller];
    }
    
    [self cqSwitchToViewControllerNoAnimation:controller];
}

/**无动画Push一个viewController*/
+ (void)cqSwitchToViewControllerNoAnimation:(UIViewController *)controller
{
    if (controller == nil)
    {
        return;
    }
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNav pushViewController:controller animated:NO];
}

/**有动画push一个viewController*/
+(void)cqSwitchToViewControllerWithAnimation:(UIViewController *)controller{
    if (controller == nil)
    {
        return;
    }
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNav pushViewController:controller animated:YES];
}

@end
